//GlobalFunc.cpp
#include "stdafx.h"
const	char 	*MeasureMenuString[]=//MEASURE_MENU_MAX_NUM*2
{
	"COMMON MEAS.",		
	"IDLE MEAS.",			
	"DUAL MEAS.",	
};
BOOL	FILEReadFromUP_R(BYTE * pByteBuf,int* pReturnLen)
{
	CFile fileRxdText;
	if(fileRxdText.Open("comm_read.txt",
						CFile::modeReadWrite  | CFile::typeBinary))
	{
		*pReturnLen=fileRxdText.GetLength();
		fileRxdText.Read((LPSTR)pByteBuf,*pReturnLen);
		fileRxdText.Close();
		return TRUE;
	}
	return FALSE;
}
BOOL	FILEReadByName(CString sFileName,BYTE * pByteBuf,int* pReturnLen)
{
	CFile fileRxdText;
	if(fileRxdText.Open(sFileName,
						CFile::modeRead  | CFile::typeBinary))
	{
		*pReturnLen=fileRxdText.GetLength();
		fileRxdText.Read((LPSTR)pByteBuf,*pReturnLen);
		fileRxdText.Close();
		return TRUE;
	}
	return FALSE;
}
void	FILEWriteToUP_T(const  BYTE pByte[],int nLen)
{
	CFile fileTxdText;
	if(fileTxdText.Open("test_check_sum.txt",CFile::modeCreate | 
						CFile::modeWrite | CFile::typeBinary|CFile::modeRead ))
	{
		fileTxdText.Write((LPCSTR)pByte,nLen);
		fileTxdText.Close();
	}
}
/*Uuencode解码*/
void unUue(unsigned char chuue[4],unsigned char chasc[3])
{// chuue：未解码的Uue代码;chasc：解码过的二进制代码
	int i,k=2;
	unsigned char t=NULL;
	if(*chuue==96) *chuue=NULL;
	else *chuue-=32;
	for(i=0;i<3;i++)
	{		
		*(chasc+i)=*(chuue+i)<<k;
		k+=2;
		if(*(chuue+i+1)==96)	*(chuue+i+1)=NULL;
		else					*(chuue+i+1)-=32;
		t=*(chuue+i+1)>>(8-k);
		*(chasc+i)|=t;
	}
}

void	Uue (unsigned char chasc[3],unsigned char chuue[4])
{//	chasc：未编码的二进制代码;chuue：编码过的Uue代码
	int i,k=2;
	unsigned char t=NULL;
	for(i=0;i<3;i++) 
	{
		*(chuue+i)=*(chasc+i)>>k;
		*(chuue+i)|=t;
		if(*(chuue+i)==NULL)	*(chuue+i)+=96;
		else					*(chuue+i)+=32;
		t=*(chasc+i)<<(8-k);
		t>>=2;
		k+=2;
	}
	*(chuue+3)=*(chasc+2)&63;
	if(*(chuue+3)==NULL) *(chuue+3)+=96;
	else				*(chuue+3)+=32;
}
int		encodeBin2Uue(BYTE *pbinBuf,BYTE	*puueBuf,int	nBinLength,UINT* pCheckSum)
{
	int	i,nLenCount,nUueLenCount;
	i=nLenCount=nUueLenCount=0;
	
	for (; nLenCount<nBinLength; )
	{
		if (nLenCount+45 >= nBinLength)//	the last line
					puueBuf[nUueLenCount++] = 32 + nBinLength - nLenCount;
		else		puueBuf[nUueLenCount++] = 'M';
		
		for (i=0;i<15;i++)
		{//将45个 bin 转化为 60个 uue 
			if(nLenCount>=nBinLength)		break;
			
			Uue(pbinBuf+nLenCount,puueBuf+nUueLenCount);
			nLenCount		+= 3;
			nUueLenCount+=4;
		}
		puueBuf[nUueLenCount++] = '\r';
		puueBuf[nUueLenCount++] = '\n';
	}
	*pCheckSum = 0;
	for (i=0;i<nBinLength;i++)
		*pCheckSum += pbinBuf[i];
	return	nUueLenCount;
}
#define	RBUF_LEN		36
void	Hex2String(BYTE *hexdata, CString &string)
{
	 BYTE cHex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	 BYTE num=0,temp1[2],i,k;
	 CString str;
	 i=hexdata[3];
	 hexdata[3]=hexdata[4];
	 hexdata[4]=i;
	 for (k=3;k<RBUF_LEN+1;k++)
	 {
			num=hexdata[k];
			for (i=0;i<2;i++)
			{
				temp1[i]=num%16;
				num=(num-num%16)/16;
			}
			str.Format ("%c%c",cHex[temp1[1]],cHex[temp1[0]]);
			if (k==5)
				string+=" -> ";
			string+=str;
	 }
}
BYTE	ASC2Hex(BYTE hi, BYTE lo, BYTE *error)
{
	if (hi>='a'&&hi<='f')
	hi=10+hi-'a';
	else if (hi>='A'&&hi<='F')
		hi=10+hi-'A';
	else if (hi>='0'&&hi<='9')
		hi=hi-'0';
	else{
		*error=1;                                  //非法字符
		return 0;
	}
	if (lo>='a'&&lo<='f')
		lo=10+lo-'a';
	else if (lo>='A'&&lo<='F')
		lo=10+lo-'A';
	else if (lo>='0'&&lo<='9')
		lo=lo-'0';
	else{
		*error=1;                                 //非法字符
		return 0;
	}
	return hi*16+lo;
}
void DoEvents()	//接收Windows消息
{
	MSG msg; 
	if (::PeekMessage(&msg,NULL, 0, 0, PM_NOREMOVE)) 
	{ 
		::GetMessage(&msg, NULL, 0, 0);
		::TranslateMessage(&msg);
		::DispatchMessage(&msg); 
	}  
}
BOOL	GetFlashSector(int  nInputStartSector,	//	几种情况调用此函数
					   int  nInputEndSector,	//1. 下载 Flash ；此时nTotalBinLen非零
					   int & nStartSector,		//2. 擦除和查空 ；此时nTotalBinLen为零
					   int & nEndSector,
					   int  nTotalBinLen)
{
	int	nEndBase;nEndBase=0;
	if((nInputStartSector>15)||(nInputEndSector>15))
		return FALSE;
	if(nTotalBinLen)
	{//1. 下载 Flash
		nEndBase = nTotalBinLen/4096;
	}
	else
	{//2. 擦除和查空
		if(nInputStartSector>nInputEndSector)//结束扇区非零，起始 应大于 结束
			return FALSE;
		if(nInputEndSector==0)	//结束扇区为零，则要操作的区域是0-7区，32K
		{
			nEndSector = 7;
			return TRUE;
		}
	}
	//1. 计算开始写入的扇区
	nStartSector = 0;
	if (nInputStartSector != 0)
		nStartSector = 7 + nInputStartSector;

	//2. 计算最大的要写入扇区
	if (nInputEndSector == 0)
	{//	输入为 0 按照实际大小计算
		nEndBase +=nInputStartSector*8 ;
		if (nEndBase<8)				//	0-7 八个扇区每个 4 K
			nEndSector = nEndBase;
		else if (nEndBase< 15*8 )	//	8-21 每个扇区占32K
			nEndSector = 7 + nEndBase/8;
		else
			nEndSector =22 + (nEndBase-15*8)/4; 
	}
	else
	{
		if(nInputEndSector<15)		nEndSector = 7 + nInputEndSector;
		else						nEndSector = 26;
	}
	return TRUE;
}
/*************************************************************
	检查最大地址范围是否出错：
				|--2132			64K
				|--2138			500K
				|--2114			120K
				|--2124			244K
	根据地址计算扇区：
				|--2138			500K    ++++++++++++_/
				|--2114			120K
				
				|--2124			244K

*************************************************************/
BOOL	GetFlashSectorByAddr( int  nInputStartAddr,	//	几种情况调用此函数
							  int  nInputEndAddr,	//1. 下载 Flash ；此时nTotalBinLen非零
							  int & nStartSector,	//2. 擦除和查空 ；此时nTotalBinLen为零
							  int & nEndSector,
							  int  nTotalBinLen)
{
	int	nEndBase;nEndBase=0;
	if((nInputStartAddr>15)||(nInputEndAddr>15))
		return FALSE;
	if(nTotalBinLen)
	{//1. 下载 Flash
		nEndBase = nTotalBinLen/4096;
	}
	else
	{//2. 擦除和查空
		if(nInputStartAddr>nInputEndAddr)//结束扇区非零，起始 应大于 结束
			return FALSE;
		if(nInputEndAddr==0)	//结束扇区为零，则要操作的区域是0-7区，32K
		{
			nEndSector = 7;
			return TRUE;
		}
	}
	return TRUE;
}

#define		SECTOR_SUM_BEFORE_64K	8
#define		SECTOR_SUM_BEFORE_8K	SECTOR_SUM_BEFORE_64K+2
#define		FLASH_SECTOR_64K	0x10000		//	64*1024
#define		FLASH_SECTOR_8K		0x30000		//	64*1024*3
//	根据输入的地址，计算该地址所在扇区
int		CalcFlash_213X(int	nAddress)
{
	int	nEndBase,nSector;nSector=nEndBase=0;
	nEndBase = nAddress/4096;

	if (nEndBase<8)				//	0-7 八个扇区每个 4 K
		nSector = nEndBase;
	else if (nEndBase< 15*8 )	//	8-21 每个扇区占32K
		nSector = 7 + nEndBase/8;
	else
		nSector =22 + (nEndBase-15*8)/4; 

	return nSector;
}
int		CalcFlash_128K(int	nAddress)
{
	return	nAddress/(8*1024);
}
int		CalcFlash_256K(int	nAddress)
{
	if (nAddress<FLASH_SECTOR_64K)
		return	nAddress/(8*1024);
	if (nAddress<FLASH_SECTOR_8K)
	{// [0x1 0000, 0x3 0000)   [64K ,192K)
		return	SECTOR_SUM_BEFORE_64K+(nAddress-FLASH_SECTOR_64K)/(64*1024);
	}
	else
	{// [0x3 0000 , 0x3 E000)  (超过 192K,小于248K)
		return	SECTOR_SUM_BEFORE_8K+(nAddress-FLASH_SECTOR_8K)/(8*1024);
	}
}